import 'package:flutterzhuyili/bean/sqlBean.dart';
import 'package:flutterzhuyili/utils/sqlUtil.dart';

/// @author xuziqiang
/// @date 2020/6/4

class RadioControlModel {
  final String radioTable = 'train_record';

  Sql trainRecordSql;

  RadioControlModel() {
    trainRecordSql = Sql.setTable(radioTable);
  }

  Future<int> getBestWholeTime(int rows, int columns, int mode) async {
    int i = 0;
    List wholeTimes = await trainRecordSql.db.rawQuery(
        'select wholeTime from $radioTable where  rows=? and  columns=? and mode=?   order by wholeTime asc  limit 0,1;',
        [rows, columns, mode]);
    wholeTimes.forEach((element) {
      i = int.parse(element['wholeTime'].toString());
    });
    return i;
  }

  Future<int> getBestHandTime(int rows, int columns, int mode) async {
    int i = 0;
    List tables = await trainRecordSql.db.rawQuery(
        'select handTime from $radioTable where  rows=? and  columns=? and mode=?   order by handTime asc  limit 0,1;',
        [rows, columns, mode]);
    tables.forEach((element) {
      i = int.parse(element['handTime'].toString());
    });
    return i;
  }

  Future<int> getBestResponseTime(int rows, int columns, int mode) async {
    int i = 0;
    List tables = await trainRecordSql.db.rawQuery(
        'select responseTime from $radioTable where  rows=? and  columns=? and mode=?   order by responseTime asc  limit 0,1;',
        [rows, columns, mode]);
    tables.forEach((element) {
      i = int.parse(element['responseTime'].toString());
    });
    return i;
  }

  // 获取所有记录信息
  Future<List<TrainRecordEntity>> getAllRecords() async {
    int i = 0;
    List list = await trainRecordSql.getByCondition();
    List<TrainRecordEntity> resultList = [];
    list.forEach((item) {
      resultList.add(TrainRecordEntity.fromJson(item));
    });
    return resultList;
  }

  // 根据条件获取
  Future<List<TrainRecordEntity>> getRecords(
      Map<String, int> conditions) async {
    List list = await trainRecordSql.getByCondition(conditions: conditions);
    List<TrainRecordEntity> resultList = [];
    list.forEach((item) {
      resultList.add(TrainRecordEntity.fromJson(item));
    });
    return resultList;
  }

  // 根据条件获取
  insertRecord(TrainRecordEntity entity) async {
    await trainRecordSql.insert({
      'responseTime': entity.responseTime,
      'handTime': entity.handTime,
      'wholeTime': entity.wholeTime,
      'rows': entity.rows,
      'columns': entity.columns,
      'mode': entity.mode,
      'date': entity.date,
    });
  }
}
